//
//  HZOrderConfirmVCModel.m
//  app
//
//  Created by 刘华舟 on 15/5/6.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZOrderConfirmVCModel.h"
#import "NSDictionary+SafetyGet.h"

@implementation HZOrderConfirmVCModel

+ (instancetype)modelWithDictionary:(NSDictionary *)dict{
    return [[HZOrderConfirmVCModel alloc] initModelWithDictionary:dict];
}
- (instancetype)initModelWithDictionary:(NSDictionary *)dict{
    if (self = [super init]) {
        
        self.noMoreData = YES;
        
        if (dict.count > 0) {
            self.noMoreData = NO;
        }
        
        self.defaultAdd = [dict dictionaryForKey:APIKEY_mall_default_add];
        
        self.addressModel = [HZOrderByerAddressModel modelWithDictionary:self.defaultAdd];
        
        
        
        //初始化配送，发票和留言
        self.pay = [dict arrayForKey:APIKEY_mall_pay_type];
        if(self.pay.count) {
            NSDictionary *payDict = [self.pay firstObject];
            if ([payDict isKindOfClass:[NSDictionary class]]) {
                self.payTypeName = [payDict stringForKey:APIKEY_mall_name nilValue:@""];
                self.payTypeNum = [payDict stringForKey:APIKEY_mall_type nilValue:@"0"];
            }else{
                self.payTypeName = @"在线支付";
                self.payTypeNum = @"0";
            }
            
            
        }
        
        self.delivery = [dict dictionaryForKey:APIKEY_mall_delivery];
        for (NSDictionary *deliDict in [self.delivery arrayForKey:APIKEY_mall_options]) {
            if ([deliDict boolForKey:APIKEY_mall_selected]) {
                self.dispatchType = [deliDict stringForKey:APIKEY_mall_title nilValue:@""];
                break;
            }
        }
        
        self.invoice = [dict dictionaryForKey:APIKEY_mall_invoice];
        
        if ([self.invoice arrayForKey:APIKEY_mall_options].count < 2) {
            self.supportInv = NO;
            self.needBill = NO;
            self.billTitle = @"";
            self.billTitleNum = @"1";
            self.billTypeType = @"0";
            self.billTypeName = @"";
        }else{
            self.supportInv = YES;
            for (NSDictionary *invoDict in [self.invoice arrayForKey:APIKEY_mall_options]) {
                if ([invoDict boolForKey:APIKEY_mall_selected]) {
                    if ([invoDict integerForKey:APIKEY_mall_id] == 1) {
                        self.needBill = NO;
                        self.billTitle = [invoDict stringForKey:APIKEY_mall_title nilValue:@""];
                    }else{
                        self.needBill = YES;
                    }
                    self.billTitleNum = [invoDict stringForKey:APIKEY_mall_id nilValue:@"1"];
                    break;
                }
            }
            
            if (self.isNeedBill) {
                self.billTitle = [[self.invoice dictForKey:@"my_invoice"] stringForKey:@"invoice_top" nilValue:@""];
            }
            
            for (NSDictionary *invTypeDict in [self.invoice arrayForKey:APIKEY_mall_invoice_type]) {
                if ([invTypeDict boolForKey:APIKEY_mall_selected]) {
                    self.billTypeType = [invTypeDict stringForKey:APIKEY_mall_type nilValue:@"0"];
                    self.billTypeName = [invTypeDict stringForKey:APIKEY_mall_name nilValue:@""];
                }
            }
        }
        
        
        NSString *payExp = [NSString stringWithFormat:@"%@ %@", self.payTypeName, self.dispatchType];
        if (self.payTypeName.length>0 &&self.dispatchType.length>0) {
            payExp = [NSString stringWithFormat:@"%@,%@", self.payTypeName, self.dispatchType];
        }
        
        HZOrderCellModel *transformModel = [HZOrderCellModel modelWithDictionary:@{@"leftTitle":payExp, @"rightTitle":@"支付配送"} type:HZOrderCellTypeThin];
        HZOrderCellModel *billModel = [HZOrderCellModel modelWithDictionary:@{@"leftTitle":self.billTitle, @"rightTitle":@"发票信息"} type:HZOrderCellTypeThin];
        self.buyerMessage = @"";
        HZOrderCellModel *messageModel = [HZOrderCellModel modelWithDictionary:@{@"leftTitle":self.buyerMessage, @"rightTitle":@"买家留言"} type:HZOrderCellTypeThin];
        
        //支持发票
        if (self.supportInv) {
            self.thinModelArray = @[transformModel, billModel, messageModel];
        }else{
            //不支持发票
            self.thinModelArray = @[transformModel, messageModel];
        }
        
        _originalPrice = @"0.00";
        
        CGFloat orgPrice = 0;
        
        //初始化商品列表
        NSMutableArray *goodsArray = [NSMutableArray array];
        NSArray *detailsArray = [dict arrayForKey:APIKEY_mall_details];
        for (NSDictionary *good in detailsArray) {
            HZOrderCellModel *model = [HZOrderCellModel modelWithDictionary:good type:HZOrderCellTypeGoods];
            orgPrice = orgPrice + [model.salePrice floatValue]*[model.productNum floatValue];
            [goodsArray addObject:model];
        }
        
        _originalPrice = [NSString stringWithFormat:@"%0.2f", orgPrice];
        self.goodsArray = goodsArray;
        
        self.gold = [dict dictionaryForKey:APIKEY_mall_gold];
        CGFloat totalP = [dict floatForKey:APIKEY_mall_total_prices nilValue:0.0f];
        self.totalPrices = [NSString stringWithFormat:@"%0.2f", totalP];
        CGFloat postA = [dict floatForKey:APIKEY_mall_postage nilValue:0.0f];
        self.postage = [NSString stringWithFormat:@"%0.2f", postA];
        //初始化金额
        NSMutableDictionary *moenyDict = [NSMutableDictionary dictionaryWithDictionary:self.gold];
        [moenyDict setObject:self.totalPrices forKey:APIKEY_mall_total_prices];
        [moenyDict setObject:self.postage forKey:APIKEY_mall_postage];
        
        self.orderHeaderModel = [HZOrderHeaderViewModel modelWithDictionary:moenyDict];
        self.orderHeaderModel.originalMoney = _originalPrice;
        
        //初始化客服
        self.customerSerArray = [dict arrayForKey:APIKEY_mall_CustomerServices];
        
        self.dispatchType = [dict stringForKey:APIKEY_mall_express_habit nilValue:@""];
        self.payTypeList = [dict arrayForKey:APIKEY_mall_payType];
        
        if (!self.payTypeList) {
            self.payTypeList = @[];
        }
        
        self.splitOrders = [dict stringForKey:APIKEY_mall_split_orders nilValue:@"1"];
        
        self.storehouse = [dict stringForKey:APIKEY_mall_storehouse nilValue:@""];
        self.houseid = [dict stringForKey:APIKEY_mall_houseid nilValue:@"-1"];
        self.cmid = [dict stringForKey:APIKEY_mall_cmid nilValue:@"-1"];
        self.cmname = [dict stringForKey:APIKEY_mall_cmname nilValue:@""];
        
        
        self.addressModel.cmid = self.cmid;
        self.addressModel.cmName = self.cmname;
        
    }
    return self;
}

//更改配送类型
- (void)switchDispatch:(NSDictionary *)delivery pay:(NSDictionary *)pay{

    //初始化配送和支付
    self.delivery = delivery;
    for (NSDictionary *deliDict in [self.delivery arrayForKey:APIKEY_mall_options]) {
        if ([deliDict boolForKey:APIKEY_mall_selected]) {
            self.dispatchType = [deliDict stringForKey:APIKEY_mall_title nilValue:@""];
            break;
        }
    }
    HZOrderCellModel *model = [self.thinModelArray objectAtIndex:0];
    
    if (self.dispatchType.length>0 && self.payTypeName.length>0) {
        model.leftTitle = [NSString stringWithFormat:@"%@,%@", self.payTypeName, self.dispatchType];
    }else{
        model.leftTitle = [NSString stringWithFormat:@"%@%@", self.payTypeName, self.dispatchType];
    }
    
}

- (void)switchBillInfo:(NSDictionary *)invocie{
    HZOrderCellModel *model = [self.thinModelArray objectAtIndex:1];
    self.invoice = invocie;
    for (NSDictionary *invoDict in [self.invoice arrayForKey:APIKEY_mall_options]) {
        if ([invoDict boolForKey:APIKEY_mall_selected]) {
            if ([invoDict integerForKey:APIKEY_mall_id] == 1) {
                self.needBill = NO;
                self.billTitle = [invoDict stringForKey:APIKEY_mall_title nilValue:@""];
            }else{
                self.needBill = YES;
            }
             self.billTitleNum = [invoDict stringForKey:APIKEY_mall_id nilValue:@"1"];
            break;
        }
    }
    
    if (self.isNeedBill) {
        self.billTitle = [[self.invoice dictForKey:@"my_invoice"] stringForKey:@"invoice_top" nilValue:@""];
    }
    
    for (NSDictionary *invTypeDict in [self.invoice arrayForKey:APIKEY_mall_invoice_type]) {
        if ([invTypeDict boolForKey:APIKEY_mall_selected]) {
            self.billTypeType = [invTypeDict stringForKey:APIKEY_mall_type nilValue:@"0"];
            self.billTypeName = [invTypeDict stringForKey:APIKEY_mall_name nilValue:@""];
        }
    }

    model.leftTitle = self.billTitle;
}

- (void)switchOrderBuyerMessage:(NSString *)message{
    NSInteger index = 1;
    if (self.isSupportInv) {
        index =2;
    }
    HZOrderCellModel *model = [self.thinModelArray objectAtIndex:index];
    self.buyerMessage = message;
    model.leftTitle = message;
}

- (void)switchDefaultAddressWithModel:(HZOrderByerAddressModel *)model{
    self.addressModel = model;
    
    self.defaultAdd = @{APIKEY_mall_id:model.addressID,
                        APIKEY_mall_nation:model.nation,
                        APIKEY_mall_province:model.province,
                        APIKEY_mall_city:model.city,
                        APIKEY_mall_district:model.district,
                        APIKEY_mall_address:model.address,
                        APIKEY_mall_phone:model.phone,
                        APIKEY_mall_receiver:model.receiver,
                        APIKEY_mall_fullAddress:model.fullAddress,
                        APIKEY_mall_selected:@"1"};
    
    
}

@end
